//
//  ZXPickerView.m
//  qianke
//
//  Created by ZX on 2017/7/3.
//  Copyright © 2017年 ZX. All rights reserved.
//

#import "ZXPickerView.h"
@interface ZXPickerView ()<UIPickerViewDelegate,UIPickerViewDataSource>

@property (nonatomic ,strong) UIView   * titleBackgroundView;/**< 标题栏背景*/
@property (nonatomic ,strong) UIButton * cancelBtn;/**< 取消按钮*/
@property (nonatomic, strong) UIButton * sureBtn;/**< 完成按钮*/

@property (nonatomic ,strong) UIPickerView   * addressPickerView;/**< 选择器*/


@end
@implementation ZXPickerView
@synthesize maskLayer, tap, control;

-(void)creatPickView:(NSArray *)arr{
    self.arr = arr;
}
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        //加载标题栏
        [self loadTitle];
        //加载选择器
        [self loadPickerView];
//        _arr = [NSArray arrayWithObjects:@"hhh",@"hhh",@"hhh",@"hhh", nil];
//        [self creatPickView:_arr];
    }
    return self;
}

#define SELFSIZE self.bounds.size
static CGFloat const TITLEHEIGHT = 50.0;
static CGFloat const TITLEBUTTONWIDTH = 75.0;

- (UIView *)titleBackgroundView{
    if (!_titleBackgroundView) {
        _titleBackgroundView = [[UIView alloc]initWithFrame:
                                CGRectMake(0, 0, SELFSIZE.width, TITLEHEIGHT)];
        _titleBackgroundView.backgroundColor = [UIColor whiteColor];
    }
    return _titleBackgroundView;
}
- (UIButton *)cancelBtn{
    if (!_cancelBtn) {
        _cancelBtn = [[UIButton alloc]initWithFrame:
                      CGRectMake(0, 0, TITLEBUTTONWIDTH, TITLEHEIGHT)];
        [_cancelBtn setTitle:@"取消"
                    forState:UIControlStateNormal];
        [_cancelBtn setTitleColor:[UIColor blueColor]
                         forState:UIControlStateNormal];
        [_cancelBtn addTarget:self
                       action:@selector(zxcancelBtnClicked)
             forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelBtn;
}

- (UIButton *)sureBtn{
    if (!_sureBtn) {
        _sureBtn = [[UIButton alloc]initWithFrame:
                    CGRectMake(SELFSIZE.width - TITLEBUTTONWIDTH, 0, TITLEBUTTONWIDTH, TITLEHEIGHT)];
        [_sureBtn setTitle:@"完成"
                  forState:UIControlStateNormal];
        [_sureBtn setTitleColor:[UIColor blueColor]
                       forState:UIControlStateNormal];
        [_sureBtn addTarget:self
                     action:@selector(zxsureBtnClicked)
           forControlEvents:UIControlEventTouchUpInside];
    }
    return _sureBtn;
}
- (UIPickerView *)addressPickerView{
    if (!_addressPickerView) {
        _addressPickerView = [[UIPickerView alloc]initWithFrame:
                              CGRectMake(0, TITLEHEIGHT, SELFSIZE.width, 165)];
        _addressPickerView.backgroundColor = [UIColor colorWithRed:239/255.f
                                                             green:239/255.f
                                                              blue:244.0/255.f
                                                             alpha:1.0];
        _addressPickerView.delegate = self;
        _addressPickerView.dataSource = self;
    }
    return _addressPickerView;
}

#pragma mark - 加载标题栏
- (void)loadTitle{
    [self addSubview:self.titleBackgroundView];
    [self.titleBackgroundView addSubview:self.cancelBtn];
    [self.titleBackgroundView addSubview:self.sureBtn];
}

#pragma mark  加载PickerView
- (void)loadPickerView{
    [self addSubview:self.addressPickerView];
}


-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return _arr.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView
             titleForRow:(NSInteger)row
            forComponent:(NSInteger)component{
    return _arr[row];
}
#pragma mark pickerView被选中
- (void)pickerView:(UIPickerView *)pickerView
      didSelectRow:(NSInteger)row
       inComponent:(NSInteger)component{
}


#pragma mark - AddressPickerViewDelegate
- (void)zxcancelBtnClicked{
    if ([_delegate respondsToSelector:@selector(zxcancelBtnClick)]) {
        [_delegate zxcancelBtnClick];
    }
}

- (void)zxshow{
//    [self addMaskLayer];
    [self zxshowOrHide:YES];
}
- (void)addMaskLayer{
    // 添加遮罩
    
    maskLayer = [CALayer layer];
    [maskLayer setFrame:[[UIScreen mainScreen] bounds]];
    [maskLayer setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.3].CGColor];
    [self.layer addSublayer:maskLayer];
    
    
    control = [[UIControl alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [control addTarget:self action:@selector(action_removeMask) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:control];
    
}

- (void)action_removeMask{
    // 移除全屏点击事件
    //    [control removeFromSuperview];
    
    // 移除遮罩
    if (maskLayer) {
        [maskLayer removeFromSuperlayer];
        [control removeFromSuperview];
    }
    }

#pragma mark  AlertView 动画

//- (void)alert_animatedIn{
//    contents.center = KeyWindow.center;
//    contents.transform = CGAffineTransformMakeScale(0.01, 0.01);
//    [KeyWindow addSubview:contents];
//    [UIView animateWithDuration:.3f animations:^{
//        contents.transform = CGAffineTransformMakeScale(1, 1);
//    }];
//}
//
//- (void)alert_animatedOut{
//    [UIView animateWithDuration:.3f animations:^{
//        contents.transform = CGAffineTransformMakeScale(0.00, 0.00);
//        
//    }];
//    maskLayer = nil;
//    contents = nil;
//}

#pragma  mark  ActionSheet 动画
//- (void)actionSheet_AnimatedIn{
//    contents.center = KeyWindow.center;
//    tmpFrame = contents.frame;
//    contents.frame = CGRectMake(0, ScreenHeight, ScreenWidth, tmpFrame.size.height);
//    [KeyWindow addSubview:contents];
//    [UIView animateWithDuration:.3f animations:^{
//        contents.frame = CGRectMake(0, ScreenHeight - tmpFrame.size.height, ScreenWidth, tmpFrame.size.height);
//    }];
//}
//
//- (void)actionSheet_animatedOut{
//    
//    [UIView animateWithDuration:.3f animations:^{
//        contents.frame = CGRectMake(0, ScreenHeight, ScreenWidth, tmpFrame.size.height);
//        
//    }];
//    maskLayer = nil;
//    contents = nil;
//}


- (void)zxhide{
    [self zxshowOrHide:NO];
}

- (void)zxshowOrHide:(BOOL)isShow{
    
    CGFloat selfY = self.frame.origin.y;
    __block CGFloat selfkY = selfY;
    [UIView animateWithDuration:0.2 animations:^{
        
        [UIView beginAnimations:@"move" context:nil];
        [UIView setAnimationDuration:0.25];
        [UIView setAnimationDelegate:self];
        //改变它的frame的x,y的值
        
        if (isShow) {
            selfkY = [UIScreen mainScreen].bounds.size.height - ZXPickerViewHeight;
        }
        else {
            selfkY = [UIScreen mainScreen].bounds.size.height;
        }
        self.frame = CGRectMake(0,selfkY, self.bounds.size.width,ZXPickerViewHeight);
        
        [UIView commitAnimations];
    }];
}

- (void)zxsureBtnClicked{
    if ([_delegate respondsToSelector:@selector(zxsureBtnClickReturnString:)]) {
        NSInteger selectString = [self.addressPickerView selectedRowInComponent:0];
//        NSInteger selectCity     = [self.addressPickerView selectedRowInComponent:1];
//        NSInteger selectArea     = [self.addressPickerView selectedRowInComponent:2];
        
        
        
        [_delegate zxsureBtnClickReturnString:_arr[selectString]];
         
    }

}

@end
